# 个体初始化 类
# 各种初始化方法 组合 生成初始种群
from C_Load_Process_Map import *
from F_Population import *
from D_Individual import *
import random
import copy
import S_Draw

class Initial:

    def __init__(self):
        # 数据集数据结构
        self.DataStructure = C_Load_Process_Map.Load_Process_Map_Me()

        print("66666666", self.DataStructure)

        # 获取订单，生产线基本信息
        self.Order_PC_Codes = self.DataStructure.Order_PC_Codes  ################################
        self.Order_PC_Codes_Len = len(self.Order_PC_Codes)
        self.Line_Type_Opens = self.DataStructure.Line_Type_Opens  ####################
        self.Line_Type_Opens_Len = len(self.Line_Type_Opens)
        print('*******self.Line_Type_Opens ', self.Line_Type_Opens)
        print('*******self.Order_PC_Codes ',self.Order_PC_Codes)
        # 初始化方法
        self.Random_Initial_Population()


    # 种群初始化方法
    # 针对X_main算法的 种群初始化方法 (非随机化，自定义的特殊种群初始化方法)
    def Initial_Population(self):
        print("模块：Initial:\t方法：Initial_Population(self):")
        Pop = Population()

        for _ in range(Pop.Pop_Size):
            Idv = Individual()
            R_Random = random.uniform(0, 1)
            if R_Random <= 0.1:
                Machine_Chromosome = self.Global_Selection_Init_Machine()
                Job_Chromosome = self.Random_Init_Job()
            elif R_Random > 0.1 and R_Random <= 0.2:
                Machine_Chromosome = self.Wordload_Considered_Init_Machine()
                Job_Chromosome = self.Random_Init_Job()
            else:
                Machine_Chromosome = self.Random_Init_Machine()
                Job_Chromosome = self.Random_Init_Job()                     

            Idv.Update_Chromosome(Job_Chromosome, Machine_Chromosome, self.PC_DataStructure,self.Resource_DataStructure)
            Pop.Population_Member.append(Idv)
        print(Pop)
        return Pop

    # 种群随机初始化方法 （随机初始化）
    def Random_Initial_Population(self):
        Pop = Population()
        for _ in range(Pop.Pop_Size):
            Idv = Individual()
            # Machine_Chromosome = self.Random_Init_Machine()
            # Job_Chromosome = self.Random_Init_Job()
            # Idv.Update_Chromosome(Job_Chromosome, Machine_Chromosome, self.DataStructure)
            # Pop.Population_Member.append(Idv)
            # S_Draw.Draw_Gatt_Me(Idv, self.DataStructure)

            x = random.randint(1, 100)
            y = random.randint(1, 100)
            Idv.Adaptability = [x, y]
            Pop.Population_Member.append(Idv)


        # Best_Idv =Individual()
        # Best_Idv.Adaptability = [999, 999, 999]
        # for i in range(len(Pop.Population_Member)):
        #     if(Pop.Population_Member[i].Adaptability[0] < Best_Idv.Adaptability[0]):
        #         Best_Idv = Pop.Population_Member[i]
        # print('Best_Idv:',Best_Idv)

        # S_Draw.Draw_Gatt_Me(Best_Idv, self.PC_DataStructure, self.Resource_DataStructure)
        # 展示原图
        x = []
        y = []
        for Idv in Pop.Population_Member:
            x.append(Idv.Adaptability[0])
            y.append(Idv.Adaptability[1])
        S_Draw.Draw_Gatt_Me_Points_2d(x, y)
        # 非支配排序
        Pop.Determine_Nodominate_Ship()
        print("非装配排序：：：：")
        print(Pop.Nodominate_Ship)
        S_Draw.Draw_Gatt_Me_Points_2d_sort(x, y, Pop.Nodominate_Ship)

        # # 不同的图示
        # x = []
        # y = []
        # z = []
        # for Idv in Pop.Population_Member:
        #     x.append(Idv.Adaptability[0])
        #     y.append(Idv.Adaptability[1])
        #     z.append(Idv.Adaptability[2])
        #
        # # S_Draw.Draw_Gatt_Me_Points_1d(x)
        # # S_Draw.Draw_Gatt_Me_Points_3d(x, y, z)
        # S_Draw.test()

        # L = C_Load_Process_DataStructure.Load_Process_Map_Me()
        # My_Decoder(L)
        # PC_DataStructure = L.PC_DataStructure
        # Resource_DataStructure = L.Resource_DataStructure

        return Pop

##########################################################################################################################

    # 随机初始化 
    # 返回随机初始化的list  工序选择染色体OS
    def Random_Init_Job(self):
        PS_Chromosome = copy.deepcopy(self.Order_PC_Codes)
        random.shuffle(PS_Chromosome)  # 随机初始化序列
        return PS_Chromosome

    # 随机初始化 
    # 返回随机初始化的list  工序选择染色体MS       
    def Random_Init_Machine(self):
        Line_Type_Opens = copy.deepcopy(self.Line_Type_Opens)
        Line_Type_Opens_Len = len(Line_Type_Opens)
        LS_Chromosome = []
        for _ in range(self.Order_PC_Codes_Len):
            Index = random.randint(0, Line_Type_Opens_Len-1)
            LS_Chromosome.append(Line_Type_Opens[Index])
        return LS_Chromosome

if __name__ == "__main__":
    Initial()

